package easier.window.popup.recycler;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager.LayoutParams;
import android.widget.PopupWindow;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import easier.window.popup.PopupWindowShow;
import easier.window.window.R;
import java.util.ArrayList;
import java.util.List;

/**
 * 显示一个popupWindow,使用recyclerView构建界面
 */
public abstract class PopupRecyclerView< T > extends PopupWindow {

    protected final RecyclerView mRecycler;

    public PopupRecyclerView( Context context ) {

        super();

        View view = LayoutInflater.from( context ).inflate( R.layout.popup_window_check_recycler, null );

        mRecycler = view.findViewById( R.id.recycler );
        mRecycler.setLayoutManager( new LinearLayoutManager( context ) );

        setContentView( mRecycler );
    }

    @Override
    public void dismiss() {

        super.dismiss();
        mRecycler.setAdapter( null );
    }

    /**
     * @param anchor 锚点
     */
    public void show( View anchor, List< T > data ) {

        mRecycler.setAdapter( new Adapter( data ) );

        PopupWindowShow.showAsDropDown(
            this,
            anchor,
            LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT,
            0, 0,
            Gravity.BOTTOM
        );
    }

    /**
     * 创建ViewHolder
     *
     * @param parent parent
     *
     * @return ViewHolder
     */
    protected abstract View createView( ViewGroup parent );

    /**
     * @param view     view
     * @param position 位置
     * @param data     数据
     */
    protected abstract void onBindView( View view, int position, T data );

    /**
     * @param v               点击了
     * @param adapterPosition 位置
     * @param item            数据
     */
    protected void notifyViewClick( View v, int adapterPosition, T item ) {

        dismiss();
    }

    /**
     * recycler adapter
     */
    protected class Adapter extends RecyclerView.Adapter< Holder > {

        protected final List< T > mData = new ArrayList<>();

        public Adapter( List< T > data ) {

            if ( data != null ) {
                mData.addAll( data );
            }
        }

        @NonNull
        @Override
        public Holder onCreateViewHolder( @NonNull ViewGroup parent, int viewType ) {

            View view = createView( parent );
            return new Holder( view );
        }

        @Override
        public void onBindViewHolder( @NonNull Holder holder, int position ) {

            holder.bind( position, mData.get( position ) );
        }

        @Override
        public int getItemCount() {

            return mData.size();
        }
    }

    /**
     * recycler Holder
     */
    protected class Holder extends RecyclerView.ViewHolder {

        private T mItem;

        public Holder( @NonNull View itemView ) {

            super( itemView );
            itemView.setOnClickListener( new OnClickListener() {
                @Override
                public void onClick( View v ) {

                    notifyViewClick( v, getAdapterPosition(), mItem );
                }
            } );
        }

        private void bind( int position, T item ) {

            mItem = item;
            onBindView( itemView, position, item );
        }
    }
}